package com.swanclouds.redfish.managers;

import com.google.common.base.Preconditions;
import com.swanclouds.api.managers.LogService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.core.transport.HttpResponse;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.common.redfish.LogOverWritePolicy;
import com.swanclouds.redfish.internal.BaseManagerService;
import com.swanclouds.redfish.managers.domain.LogModel;
import com.swanclouds.redfish.managers.domain.LogServicesListModel;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

@SuppressWarnings("all")
public class LogServiceImpl extends BaseManagerService implements LogService {
    /**
     * 日志服务信息资源集合查询
     * @return CollectionResource
     */
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class, uri(ClientConstants.PATH_MANAGERS_LOGSERVICE, getManagerId())).execute();
    }

    /**
     * 查询日志服务Entries信息
     * @param entriesId 日志类型字符 如SSH 必填参数
     * @return LogServicesListModel
     */
    @Override
    public LogServicesListModel entries(String entriesId) {
        Preconditions.checkNotNull(entriesId, "EntriesId is null");
        return get(LogServicesListModel.class,uri(ClientConstants.PATH_MANAGERS_LOGSERVICE_ENTRIES,getManagerId(),entriesId)).execute();
    }

    /**
     * 查询服务器的日志资源集合实例信息
     * @param entriesId 日志id 必填参数
     * @param dataId 日志类型如SSH 必填参数
     * @return LogServicesListModel
     */
    @Override
    public LogServicesListModel entriesId(String dataId ,String entriesId) {
        Preconditions.checkNotNull(entriesId, "EntriesId is null");
        return get(LogServicesListModel.class,uri(ClientConstants.PATH_MANAGERS_LOGSERVICE_ENTRIES_ID,getManagerId(),dataId,entriesId)).execute();
    }

    /**
     * 查询指定的日志信息
     * @param dataId  所查询日志类型字符 如SSH 必填参数
     * @return LogModel
     */
    public LogModel getLogServicesMembers(String dataId) {
        Preconditions.checkNotNull(dataId, "dataId is null");
        return get(LogModel.class,uri(ClientConstants.PATH_MANAGERS_LOGSERVICE_MEMBERS,getManagerId(),dataId)).execute();
    }

    /**
     * 清除指定的日志资源信息
     * @param memberId 指定清楚的日志资源信息 如SHH 必填参数
     * @return ActionResponse
     */
    @Override
    public ActionResponse clearLog(String memberId) {
        Preconditions.checkNotNull(memberId, "memberId is null");
        return post(ActionResponse.class,uri(ClientConstants.PATH_MANAGERS_SNMPSERVICE_CLEARLOG,getManagerId(),memberId)).execute();
    }

    /**
     * 服务器设置日志存储策略
     * @param logOverWritePolicy 日志的存储策略，包括：NeverOverWrites  WrapsWhenFull
     * @return LogOverWritePolicy
     */
    @Override
    public LogOverWritePolicy setOverWritePolicy(LogOverWritePolicy.OverWritePolicy logOverWritePolicy) {
        Preconditions.checkNotNull(logOverWritePolicy, "logOverWritePolicy is null");
        return post(LogOverWritePolicy.class,uri(ClientConstants.PATH_MANAGERS_SNMPSERVICE_OVERWRITEPOLICY,getManagerId())).entity(logOverWritePolicy).execute();
    }

    /**
     * 下载服务器的日志资源信息
     * @param memberId 指定清楚的日志资源信息 如SHH 必填参数
     * @return ActionResponse
     */
    @Override
    public ActionResponse download(String memberId) {
        Preconditions.checkNotNull(memberId, "memberId is null");
        HttpResponse response = post(Void.class, uri(ClientConstants.PATH_MANAGERS_SNMPSERVICE_DOWNLOADLOG, getManagerId(),memberId))
                .executeWithResponse();
        if (response.getStatus() < 400) {
            InputStream inputStream = response.getInputStream();
            OutputStream outputStream;
            try {
               outputStream = new FileOutputStream("");
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                inputStream.close();
                outputStream.flush();
                outputStream.close();
                return ActionResponse.actionSuccess();
            } catch (Exception e) {
                e.printStackTrace();
                return ActionResponse.actionFailed("Failed to write to file " + e.getMessage(), 400);
            }
        } else {
            return ActionResponse.actionFailed(response.getStatusMessage(),response.getStatus());
        }
    }
}
